package basisApi

import (
	"github.com/Sir-yuChen/funAnalyse/global"
	"github.com/Sir-yuChen/funAnalyse/model/common/ApiReturn"
	"github.com/Sir-yuChen/funAnalyse/model/common/response"
	"github.com/Sir-yuChen/funAnalyse/model/request"
	"github.com/Sir-yuChen/funAnalyse/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type UserApi struct{}

// UserLogin 用户登录
// @Summary 用户登录
// @Tags 用户相关接口
// @Accept application/json
// @Produce application/json
// @Param data body request.UserLogin true "请求参数"
// @response 200 {object} response.Response{data=resp.UserResponse} "返回用户信息"
// @Router /funAnalyse/user/userLogin [Post]
func (b *UserApi) UserLogin(c *gin.Context) {
	//从请求中获取参数转为json
	var userLogin request.UserLogin
	if err := c.ShouldBindJSON(&userLogin); err != nil {
		utils.ErrValidatorResp(err, "AutoCode", userLogin, c)
		return
	}
	// 登录主逻辑
	apiReturn := userService.UserLogin(&userLogin, c)
	response.WithApiReturn(apiReturn, c)
	global.GVA_LOG.Info("用户登录", zap.Any("CustomerName", userLogin.CustomerName))
	return
}

// UserLoginOut 退出
// @Summary 退登
// @Tags 用户相关接口
// @Accept application/json
// @Produce application/json
// @response 200 {object} response.Response "返回用户信息"
// @Router /funAnalyse/user/userLoginOut [GET]
func (b *UserApi) UserLoginOut(c *gin.Context) {
	apiReturn := userService.UserLoginOut(c)
	response.WithApiReturn(apiReturn, c)
	return
}

// UserRegister 用户注册
// @Summary 用户注册
// @Tags 用户相关接口
// @Accept application/json
// @Produce application/json
// @Param data body request.UserRegister true "请求参数"
// @response 200 {object} response.Response{data=resp.UserResponse} "返回用户信息"
// @Router /funAnalyse/user/userRegister [Post]
func (b *UserApi) UserRegister(c *gin.Context) {
	var registerInfo request.UserRegister
	err := c.ShouldBindJSON(&registerInfo)
	if err != nil {
		utils.ErrValidatorResp(err, "AutoCode", registerInfo, c)
		return
	}
	apiReturn := userService.UserRegister(&registerInfo)
	response.WithApiReturn(apiReturn, c)
}

//GetUserInfo 查用户详情
// @Summary 查用户详情
// @Tags 用户相关接口
// @Accept application/json
// @Produce application/json
// @Security ApiKeyAuth
// @Param data query request.UserInfoById true "用户ID"
// @response 200 {object} response.Response{data=model.StockUser} "返回用户信息"
// @Router /funAnalyse/user/getUserInfo [Get]
func (b *UserApi) GetUserInfo(c *gin.Context) {
	customerId := c.Query("CustomerId")
	if customerId == "" {
		response.WithApiReturn(ApiReturn.ErrParam, c)
		return
	}
	if ReqUser, err := userService.GetUserInfo(customerId); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "成功", c)
	}
}

// UpateUserInfo 修改用户信息[手机号/密码]
// @Summary 修改用户信息[手机号/密码]
// @Tags 用户相关接口
// @Accept application/json
// @Produce application/json
// @Param data body request.UpdateUserInfo true "请求参数"
// @response 200 {object} response.Response "返回用户信息"
// @Router /funAnalyse/user/upateUserInfo [Post]
func (b *UserApi) UpateUserInfo(c *gin.Context) {
	var req request.UpdateUserInfo
	err := c.ShouldBindJSON(&req)
	if err != nil {
		utils.ErrValidatorResp(err, "UpateUserInfo", req, c)
		return
	}
	apiReturn := userService.UpateUserInfo(&req, c)
	response.WithApiReturn(apiReturn, c)
}

// GetUserList 查用户集合或导出
// @Summary 查用户集合或导出
// @Tags 用户相关接口
// @Accept application/json
// @Produce application/json
// @Param data body request.UpdateUserInfo true "请求参数"
// @response 200 {object} response.Response "返回用户信息"
// @Router /funAnalyse/user/getUserList [Get]
func (b *UserApi) GetUserList(c *gin.Context) {
	var req request.UserInfoList
	err := c.ShouldBind(&req)
	if err != nil {
		utils.ErrValidatorResp(err, "GetUserList", req, c)
		return
	}
	apiReturn := userService.GetUserList(&req, c)
	response.WithApiReturn(apiReturn, c)
}
